function _set_element(item, _id, _value, _type) {
    if (!item) {
        return;
    }
    item.setAttribute("id", _id);
    item.setAttribute("value", _value);
    item.setAttribute("type", _type);
}

function debug(src) {
    var output = document.getElementById('outputArea');
    output.value = output.value + src+"\n";
}

Wepye_Control_Bar={}
Wepye_Control_Bar.get_source = function () {
    var editor = document.getElementById('editTextArea');
    if (!editor) {
        this.autosave_timeout = window.setTimeout(Wepye_Control_Bar.get_source, 1000);
        return;
    }
    src = editor.value;
    this.autosave_timeout = window.setTimeout(Wepye_Control_Bar.get_source, 1000);
    return src;
}
Wepye_Control_Bar.run_script_cb = function () {
    var src = Wepye_Control_Bar.get_source();
    var request=new XMLHttpRequest();
    var query = "src="+encodeURIComponent(src);
    request.open("POST", "runScript", true);
    request.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    request.send(query);
    Wepye_Output_Field.get_output_from_server();
}
Wepye_Control_Bar.stop_script_cb = function () {
    var request=new XMLHttpRequest();
    request.open("POST", "stopScript", true);
    request.send("src=");
}
Wepye_Control_Bar.View_init = function (div) {
    var src="";
    var newFile = document.createElement('input');
        _set_element(newFile, "newFile", "New", "button");
    var openFile = document.createElement('input');
        _set_element(openFile, "openFile", "Open", "button");
    var saveFile = document.createElement('input');
        _set_element(saveFile, "saveFile", "Save", "button");
    var commitSCM = document.createElement('input');
        _set_element(commitSCM, "saveSCM", "Commit", "button");
    var delFile = document.createElement('input');
        _set_element(delFile, "delFile", "Del", "button");
    var runScript = document.createElement('input');
        _set_element(runScript, "runScript", "Run", "button");
    var stopScript = document.createElement('input');
        _set_element(stopScript, "stopScript", "Stop", "button");
    div.appendChild(newFile);
    div.appendChild(openFile);
    div.appendChild(saveFile);
    div.appendChild(commitSCM);
    div.appendChild(delFile);
    div.appendChild(runScript);
    div.appendChild(stopScript);

    var autosave_timeout = window.setTimeout(Wepye_Control_Bar.get_source, 1000);


    runScript.onclick = this.run_script_cb;
    stopScript.onclick = this.stop_script_cb;
}
Wepye_Edit_Field={}
Wepye_Edit_Field.View_init = function (div) {
    var editTextArea = document.createElement('textarea');
        _set_element(editTextArea, "editTextArea", "", "textarea");
        editTextArea.setAttribute("rows", "16");
        editTextArea.setAttribute("cols", "60");
    div.appendChild(editTextArea);
}

Wepye_Output_Field={}
Wepye_Output_Field.res = null;
Wepye_Output_Field.get_output_from_server = function () {
    if (!Wepye_Output_Field.res) {
        Wepye_Output_Field.res = new XMLHttpRequest();
        Wepye_Output_Field.res.open("POST", "getOutput", true);
        Wepye_Output_Field.res.send("src=12345");
        window.setTimeout(Wepye_Output_Field.get_output_from_server, 300);
        return;
    }
    var res = Wepye_Output_Field.res;
    var output = document.getElementById('outputArea');
    var src;
    var endindex=-1
  
    if (res.readyState==4 && res.status==200) {
        de=res.responseXML.documentElement;
        if ( de.tagName!="idem" ) {
            src = output.value + Sarissa.stripTags(res.responseText);
            endindex = src.search('#WEPYE_END_OF_CHILD_PROCESS')
            if (endindex >= 0) {
                src = src.substr(0, endindex);
            }
            output.value = src;
            output.scrollTop = output.scrollHeight;
        }
        Wepye_Output_Field.res = null;
    }

    if (endindex < 0) {
        window.setTimeout(Wepye_Output_Field.get_output_from_server, 100);
    }
    return output.value;
}
Wepye_Output_Field.View_init = function (div) {
    var outputArea = document.createElement('textarea');
        _set_element(outputArea, "outputArea", "", "textarea");
        outputArea.setAttribute("rows", "4");
        outputArea.setAttribute("cols", "60");
        outputArea.setAttribute("readOnly", "true");
    div.appendChild(outputArea);
}

Wepye={};
Wepye.View_control = function (id) {
    var ie=0;
    if (window.ActiveXObject)
        ie=1;
    var div=document.getElementById(id);
    var controlBar = document.createElement('div');
    var editDiv = document.createElement('div');
    var outputDiv = document.createElement('div');
    

    function init() {
        Wepye_Control_Bar.View_init(controlBar);
        Wepye_Edit_Field.View_init(editDiv);
        Wepye_Output_Field.View_init(outputDiv);

        div.appendChild(controlBar);
        div.appendChild(editDiv);
        div.appendChild(outputDiv);
    }
    init();
}

Wepye.View  = function (id) {
    return new this.View_control(id);
}
